import {login, logout} from "@/api/login";
import {LoginForm} from "@/api/login/types";
import {getAdminInfo} from "@/api/admin";
import {removeToken, setToken} from "@/utils/token";
import {defineStore} from "pinia";
import {AdminState} from "../interface";

const useAdminStore = defineStore("useAdminStore", {
    state: (): AdminState => ({
        id: null,
        avatar: "",
        roleList: [],
        permissionList: [],
    }),
    actions: {
        LogIn(LoginForm: LoginForm) {
            return new Promise((resolve, reject) => {
                login(LoginForm)
                    .then(({data}) => {
                        // 请求成功时被调用
                        if (data.flag) {
                            setToken(data.data);
                            resolve(data);
                        } else {
                            reject(data.msg);
                        }
                    })
                    .catch((error) => {
                        reject(error);
                    });
            });
        },
        GetInfo() {
            return new Promise((resolve, reject) => {
                getAdminInfo()
                    .then(({data}) => {
                        if (data.flag) {
                            this.id = data.data.id;
                            this.avatar = data.data.avatar;
                            this.roleList = data.data.roleList;
                            this.permissionList = data.data.permissionList;
                        }
                        resolve(data);
                    })
                    .catch((error) => {
                        reject(error);
                    });
            });
        },
        LogOut() {
            return new Promise((resolve, reject) => {
                logout()
                    .then(() => {
                        this.id = null;
                        this.avatar = "";
                        this.roleList = [];
                        this.permissionList = [];
                        removeToken();
                        resolve(null);
                    })
                    .catch((error) => {
                        reject(error);
                    });
            });
        },
    },
    getters: {},
});

export default useAdminStore;
